package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.MessageCollectVideoadapter;
import dhm.com.hpx.adapter.MessageColletArticleadapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.FavoriteArticleBean;
import dhm.com.hpx.entity.FavoriteVideoBean;
import dhm.com.hpx.utils.SpUtils;

public class CollectionActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.edit)
    TextView edit;
    @BindView(R.id.text_like)
    TextView textLike;
    @BindView(R.id.line_like)
    TextView lineLike;
    @BindView(R.id.text_liked)
    TextView textLiked;
    @BindView(R.id.line_liked)
    TextView lineLiked;
    @BindView(R.id.recy)
    XRecyclerView recy;

    int page=1;

    private PressenterImpl pressenter;
    private String uid;

    @Override
    protected int getLayout() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recy.setLayoutManager(manager);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);
        page=1;

        getColletVideo();
    }

    String type="2";
    private void clearcollection() {
        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("type",type);
        pressenter.sendMessage(this,Constant.clear_del,map, Bean.class);
    }



    private void getColletVideo() {
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getColletVideo();
            }

            @Override
            public void onLoadMore() {
                getColletVideo();
            }
        });

        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");
        pressenter.sendMessage(this,Constant.listVideoQuery,map, FavoriteVideoBean.class);
    }

    private void getColletArticle() {
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getColletArticle();
            }

            @Override
            public void onLoadMore() {
                getColletArticle();
            }
        });

        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");
        pressenter.sendMessage(this,Constant.listArticlesQuery,map, FavoriteArticleBean.class);
    }



    @Override
    protected void initData() {

    }

    @SuppressLint("NewApi")
    @OnClick({R.id.back, R.id.edit, R.id.video, R.id.article})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit:
                clearcollection();
                break;
            case R.id.video:
                type="2";
                textLike.setTextColor(getResources().getColor(R.color.main));
                textLiked.setTextColor(Color.parseColor("#666666"));
                lineLike.setVisibility(View.VISIBLE);
                lineLiked.setVisibility(View.INVISIBLE);
                page=1;
                getColletVideo();
                break;
            case R.id.article:
                type="1";
                textLiked.setTextColor(getResources().getColor(R.color.main));
                textLike.setTextColor(Color.parseColor("#666666"));
                lineLiked.setVisibility(View.VISIBLE);
                lineLike.setVisibility(View.INVISIBLE);
                page=1;
                getColletArticle();
                break;
                default:
        }
    }
    private List<FavoriteVideoBean.DataBean> favoriteVideo;
    private MessageCollectVideoadapter messageCollectVideoadapter;
    private List<FavoriteArticleBean.DataBean> favoriteArticle;
    private MessageColletArticleadapter messageColletArticleadapter;

    @Override
    public void requesta(Object data) {
        if (data instanceof FavoriteVideoBean){
            FavoriteVideoBean listBean= (FavoriteVideoBean) data;
            if (listBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    favoriteVideo = listBean.getData();
                    messageCollectVideoadapter = new MessageCollectVideoadapter(this);
                    messageCollectVideoadapter.setdeleteClick(new MessageCollectVideoadapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            if (favoriteVideo.get(posttion).getFavoriteType()==2){
                                Intent intent=new Intent(CollectionActivity.this, VideoActivity.class);
                                intent.putExtra("video_id",favoriteVideo.get(posttion).getVideoId()+"");
                                intent.putExtra("coverImg",favoriteVideo.get(posttion).getCoverImg()+"");
                                startActivity(intent);
                            }else {
                                Intent intent=new Intent(CollectionActivity.this, ExtensionradioActivity.class);
                                intent.putExtra("extension_id",favoriteVideo.get(posttion).getVideoId()+"");
                                startActivity(intent);
                            }

                        }
                    });
                    messageCollectVideoadapter.setaddClick(new MessageCollectVideoadapter.OnIntentClick() {
                        @Override
                        public void item(int position) {
                            Map<String,String> map=new HashMap<>();
                            map.put("token",Constant.TOKEN);
                            map.put("uid",uid);
                            map.put("type","2");
                            map.put("cid",favoriteVideo.get(position).getFavoriteId()+"");

                            pressenter.sendMessage(CollectionActivity.this,Constant.cancel,map, Bean.class);
                            favoriteVideo.remove(position);
                            messageCollectVideoadapter.setList(favoriteVideo);
                        }
                    });

                    recy.setAdapter(messageCollectVideoadapter);
                }else {
                    favoriteVideo.addAll(listBean.getData());
                }
                messageCollectVideoadapter.setList(favoriteVideo);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, listBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            recy.refreshComplete();
            recy.loadMoreComplete();
        }else if (data instanceof FavoriteArticleBean){
            FavoriteArticleBean listBean= (FavoriteArticleBean) data;
            if (listBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    favoriteArticle = listBean.getData();
                    messageColletArticleadapter = new MessageColletArticleadapter(this);
                    messageColletArticleadapter.setdeleteClick(new MessageColletArticleadapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            if (favoriteArticle.get(posttion).getFavoriteType()==1){
                                Intent intent=new Intent(CollectionActivity.this, ArticleActivity.class);
                                intent.putExtra("article_id",favoriteArticle.get(posttion).getArticleId()+"");
                                intent.putExtra("coverImg",favoriteArticle.get(posttion).getCoverImg()+"");
                                startActivity(intent);
                            }else {
                                Intent intent=new Intent(CollectionActivity.this, ExtensionarticleActivity.class);
                                intent.putExtra("extension_id",favoriteArticle.get(posttion).getArticleId()+"");
                                startActivity(intent);
                            }


                        }
                    });
                    messageColletArticleadapter.setaddClick(new MessageColletArticleadapter.OnIntentClick() {
                        @Override
                        public void item(int position) {
                            Map<String,String> map=new HashMap<>();
                            map.put("token",Constant.TOKEN);
                            map.put("uid",uid);
                            map.put("type","1");
                            map.put("cid",favoriteArticle.get(position).getFavoriteId()+"");

                            pressenter.sendMessage(CollectionActivity.this,Constant.cancel,map,Bean.class);
                            favoriteArticle.remove(position);
                            messageColletArticleadapter.setList(favoriteArticle);
                        }
                    });

                    recy.setAdapter(messageColletArticleadapter);
                }else {
                    favoriteArticle.addAll(listBean.getData());
                }
                messageColletArticleadapter.setList(favoriteArticle);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, listBean.getMessage(), Toast.LENGTH_SHORT).show();
            }

            recy.refreshComplete();
            recy.loadMoreComplete();
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                if (bean.getMessage().equals("清空成功")){
                    favoriteVideo.clear();
                    favoriteArticle.clear();
                    messageCollectVideoadapter.setList(favoriteVideo);
                    messageColletArticleadapter.setList(favoriteArticle);
                }
            }


        }
    }

    @Override
    public void fail(String error) {

    }
}
